% by sby 6/15
% by clq
% 2021/6/12
function[i_R,profits_R,EYI,EYin,ProfitPart] = round_robin(j_all,N)
    addpath functions;
    global I J P
    patient_number = size(j_all,2);
    N = min(patient_number,N);%N不能超过给了病种的病人数。
    profits_R = zeros(1,N);
    ProfitPart = zeros(3,N);
    EYI = zeros(1, N);
    EYin = zeros(1, N);
    %j_all is the given order of patient types
    S = zeros(I,J);
    i_R = zeros(1,N);
    Q = [ones(I,1),zeros(I,N)];
    R = [ones(I,1),zeros(I,N)];
    %Step1:find the optimal slot for each patient
    for n=1:N
        i_R(1,n) = mod(n-1,I)+1; 
    %Step2:find the profit after each patient is assigned
        j = j_all(n);
        S(i_R(1,n),j) = S(i_R(1,n),j)+1;
        Q = findQ2(Q,i_R(1,n),j,P,n); 
        R = findR2(Q,i_R(1,n),R,n);
        [profits_R(n),EYI,EYin,ProfitPart] = function_fQR(Q,R,EYI,EYin,ProfitPart,n);
    end
return




